﻿[{
    "name": "HostName",
    "desc": "Creates a new HostName object from a string that contains a hostname or an IP address",
    "source": function () {
        var hostName = new Windows.Networking.HostName("microsoft.com");
        console.log("Canonical Name", hostName.canonicalName);
        console.log("Display Name", hostName.displayName);
        console.log("Network Adapter", hostName.networkAdapter);
        console.log("Display Name", hostName.rawName);
    }
},
{
    "name": "Network Adapters",
    "desc": "Gets a list of profiles for connections currently established on the local machine.",
    "source": function () {
        var connectionProfiles = Windows.Networking.Connectivity.NetworkInformation.getConnectionProfiles();
        for (var i = 0; i < connectionProfiles.size; i++) {
            console.log("==== " + connectionProfiles[i].profileName + " =====");
            console.log("Connected" + connectionProfiles[i].connected);
            var networkAdapter = connectionProfiles[i].networkAdapter;
            console.log("Network Adapter ID: ", networkAdapter.ianaInterfaceType + networkAdapter.networkAdapterId);
            console.log("Speed: Inbound", networkAdapter.inboundMaxBitsPerSecond + " Outbound" + networkAdapter.outboundMaxBitsPerSecond);
        }
    }
},
{
    "name": "Connection Cost Info",
    "desc": "Gets the cost information for the connection.",
    "source": function () {
        var networkInfo = Windows.Networking.Connectivity.NetworkInformation;
        try {
            // get the ConnectionProfile that is currently used to connect to the Internet
            var obj = [];
            var connectionProfile = networkInfo.getInternetConnectionProfile();
            if (connectionProfile == null) {
                console.log("Connection Profile not found");
            }
            obj.push("ProfileName: " + connectionProfile.profileName);
            obj.push("Connected: " + connectionProfile.connected);
            //Display Connection cost info
            var conncostinfo = []
            var connectionCost = connectionProfile.getConnectionCost();
            var msg = ["unknown", "unrestricted", "fixed", "variable"];
            conncostinfo.push("Cost Type: " + msg[connectionCost.networkCostType]);
            conncostinfo.push("Roaming: " + connectionCost.roaming);
            conncostinfo.push("Over Datalimit: " + connectionCost.overDataLimit);
            conncostinfo.push("Approaching Datalimit: " + connectionCost.approachingDataLimit);
            obj.push(conncostinfo);
            console.log(obj);
        }
        catch (e) {
            console.log("Exception Caught: " + e + "\n\r");
        }
    }
},
{
    "name": "Data Plan Status",
    "desc": "Gets the current status of the data plan associated with the connection.",
    "source": function () {
        var networkInfo = Windows.Networking.Connectivity.NetworkInformation;
        try {
            // get the ConnectionProfile that is currently used to connect to the Internet
            var obj = [];
            var connectionProfile = networkInfo.getInternetConnectionProfile();
            if (connectionProfile == null) {
                console.log("Connection Profile not found");
            }
            obj.push("ProfileName: " + connectionProfile.profileName);
            obj.push("Connected: " + connectionProfile.connected);

            var dataplanstatus = [];
            var dataPlanStatus = connectionProfile.getDataPlanStatus();
            if (dataPlanStatus.dataPlanUsage != null) {
                dataplanstatus.push("Usage In Megabytes: " + dataPlanStatus.dataPlanUsage.megabytesUsed);
                dataplanstatus.push("Last Sync Time: " + dataPlanStatus.dataPlanUsage.lastSyncTime);
            }
            else {
                dataplanstatus.push("Dataplan Usage: " + "Not Defined");
            }

            if (dataPlanStatus.InboundBitsPerSecond != null) {
                dataplanstatus.push("Inbound Bits Per Second: " + dataPlanStatus.InboundBitsPerSecond);
            }
            else {
                dataplanstatus.push("Inbound Bits Per Second: " + "Not Defined");
            }

            if (dataPlanStatus.OutboundBitsPerSecond != null) {
                dataplanstatus.push("Outbound Bits Per Second: " + dataPlanStatus.OutboundBitsPerSecond);
            }
            else {
                dataplanstatus.push("Outbound Bits Per Second: " + "Not Defined");
            }

            if (dataPlanStatus.dataLimitInMegabytes != null) {
                dataplanstatus.push("Data Limit In Megabytes: " + dataPlanStatus.dataLimitInMegabytes);
            }
            else {
                dataplanstatus.push("Data Limit In Megabytes: " + "Not Defined");
            }

            if (dataPlanStatus.nextBillingCycle != null) {
                dataplanstatus.push("Next Billing Cycle: " + dataPlanStatus.nextBillingCycle);
            }
            else {
                dataplanstatus.push("Next Billing Cycle: " + "Not Defined");
            }

            if (dataPlanStatus.maxDownloadFileSizeInMegabytes != null) {
                dataplanstatus.push("Maximum Download File Size in Megabytes: " + dataPlanStatus.maxDownloadFileSizeInMegabytes);
            }
            else {
                dataplanstatus.push("Maximum Download File Size in Megabytes: " + "Not Defined");
            }

            obj.push(dataplanstatus);
            console.log(obj);
        }

        catch (e) {
            console.log("Exception Caught: " + e + "\n\r");
        }
    }
},
{
    "name": "Data Usage",
    "desc": "Gets the local usage data for a connection during a specific time interval",
    "source": function () {
        var networkInfo = Windows.Networking.Connectivity.NetworkInformation;
        try {
            var obj = [];
            var connectionProfile = networkInfo.getInternetConnectionProfile();
            if (connectionProfile == null) {
                console.log("Connection Profile not found");
            }
            obj.push("ProfileName: " + connectionProfile.profileName);
            obj.push("Connected: " + connectionProfile.connected);

            var currTime = new Date();

            //Set start Time to 1 hour (3600000ms) before current time
            var startTime = new Date(currTime - 3600000);

            var LocalUsage = connectionProfile.getLocalUsage(startTime, currTime);

            var usage = [];

            usage.push("Bytes Sent: " + LocalUsage.bytesSent);
            usage.push("Bytes Received: " + LocalUsage.bytesReceived);
            obj.push(usage);
            console.log(obj);
        }

        catch (e) {
            console.log("Exception Caught: " + e + "\n\r");
        }
    }
},
{
    "name": "Connection Profiles",
    "desc": "Gets the Connection Cost Information, Data Plan and Data Usage information for each of the profiles",
    "source": function () {
        var networkInfo = Windows.Networking.Connectivity.NetworkInformation;     
        var obj = [];
        try {
            var ConnectionProfiles = networkInfo.getConnectionProfiles();
            if (ConnectionProfiles.length != 0) {
                for (var i = 0; i < ConnectionProfiles.length; i++) {
                    var profile = [];
                    //Display Connection profile info for each profile
                    var connectionProfile = ConnectionProfiles[i];
                    if (connectionProfile == null) {
                        console.log("Connection Profile not found");
                    }
                    profile.push("ProfileName: " + connectionProfile.profileName);
                    profile.push("Connected: " + connectionProfile.connected);
                    profile.push(GetConnectionCost(connectionProfile));
                    profile.push(GetDataPlan(connectionProfile));
                    profile.push(GetUsageData(connectionProfile));

                    obj.push(profile);
                }
                console.log(obj);
            }
            else {
                console.log("No profiles found");
            }
        }
        catch (e) {
            console.log("Exception Caught: " + e + "\n\r");
        }
        
        function GetConnectionCost(connectionProfile) {
            try {
                if (connectionProfile == null) {
                    console.log("Connection Profile not found");
                }
                //Display Connection cost info
                var conncostinfo = []
                var connectionCost = connectionProfile.getConnectionCost();
                var msg = ["unknown", "unrestricted", "fixed", "variable"];
                conncostinfo.push("Cost Type: " + msg[connectionCost.networkCostType]);
                conncostinfo.push("Roaming: " + connectionCost.roaming);
                conncostinfo.push("Over Datalimit: " + connectionCost.overDataLimit);
                conncostinfo.push("Approaching Datalimit: " + connectionCost.approachingDataLimit);
                return conncostinfo;
            }
            catch (e) {
                console.log("Exception Caught: " + e + "\n\r");
            }
        }

        function GetDataPlan(connectionProfile) {
            try {
                if (connectionProfile == null) {
                    console.log("Connection Profile not found");
                }
                var dataplanstatus = [];
                var dataPlanStatus = connectionProfile.getDataPlanStatus();
                if (dataPlanStatus.dataPlanUsage != null) {
                    dataplanstatus.push("Usage In Megabytes: " + dataPlanStatus.dataPlanUsage.megabytesUsed);
                    dataplanstatus.push("Last Sync Time: " + dataPlanStatus.dataPlanUsage.lastSyncTime);
                }
                else {
                    dataplanstatus.push("Dataplan Usage: " + "Not Defined");
                }

                if (dataPlanStatus.InboundBitsPerSecond != null) {
                    dataplanstatus.push("Inbound Bits Per Second: " + dataPlanStatus.InboundBitsPerSecond);
                }
                else {
                    dataplanstatus.push("Inbound Bits Per Second: " + "Not Defined");
                }

                if (dataPlanStatus.OutboundBitsPerSecond != null) {
                    dataplanstatus.push("Outbound Bits Per Second: " + dataPlanStatus.OutboundBitsPerSecond);
                }
                else {
                    dataplanstatus.push("Outbound Bits Per Second: " + "Not Defined");
                }

                if (dataPlanStatus.dataLimitInMegabytes != null) {
                    dataplanstatus.push("Data Limit In Megabytes: " + dataPlanStatus.dataLimitInMegabytes);
                }
                else {
                    dataplanstatus.push("Data Limit In Megabytes: " + "Not Defined");
                }

                if (dataPlanStatus.nextBillingCycle != null) {
                    dataplanstatus.push("Next Billing Cycle: " + dataPlanStatus.nextBillingCycle);
                }
                else {
                    dataplanstatus.push("Next Billing Cycle: " + "Not Defined");
                }

                if (dataPlanStatus.maxDownloadFileSizeInMegabytes != null) {
                    dataplanstatus.push("Maximum Download File Size in Megabytes: " + dataPlanStatus.maxDownloadFileSizeInMegabytes);
                }
                else {
                    dataplanstatus.push("Maximum Download File Size in Megabytes: " + "Not Defined");
                }

                return dataplanstatus;
            }
            catch (e) {
                console.log("Exception Caught: " + e + "\n\r");
            }
        }

        function GetUsageData(connectionProfile) {
            try {
                if (connectionProfile == null) {
                    console.log("Connection Profile not found");
                }
                var currTime = new Date();
                //Set start Time to 1 hour (3600000ms) before current time
                var startTime = new Date(currTime - 3600000);
                var LocalUsage = connectionProfile.getLocalUsage(startTime, currTime);
                var usage = [];
                usage.push("Bytes Sent: " + LocalUsage.bytesSent);
                usage.push("Bytes Received: " + LocalUsage.bytesReceived);
                return usage;
            }

            catch (e) {
                console.log("Exception Caught: " + e + "\n\r");
            }
        }

    }
}
]